#include "echo.h"
#include "../../include/Logger.h"

void EchoServer::OnConnected(const PtrConnection &conn)
{
    DBG_LOG("新连接链接成功：%p", conn.get());
}

void EchoServer::OnClosed(const PtrConnection &conn)
{
    DBG_LOG("关闭连接：%p", conn.get());
}

void EchoServer::OnMessage(const PtrConnection &conn, Buffer *buf)
{
    conn->Send(buf->ReadPosition(), buf->ReadAbleSize());
    buf->MoveReadOffset(buf->ReadAbleSize());
    conn->ShutDown();
}

EchoServer::EchoServer(int port)
    : _server(port)
{
    _server.SetThreadCount(2);
    _server.EnableInactiveRelease(10);
    _server.SetClosedCallback(std::bind(&EchoServer::OnClosed, this, std::placeholders::_1));
    _server.SetConnectedCallback(std::bind(&EchoServer::OnConnected, this, std::placeholders::_1));
    _server.SetMessageCallback(std::bind(&EchoServer::OnMessage, this, std::placeholders::_1, std::placeholders::_2));
}

void EchoServer::Start()
{
    _server.Start();
}